import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:sports_new/controller/service/config_service.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/widgets/base/qkd_widget.dart';
import 'package:sports_new/widgets/open_notification_dialog.dart';

import '../../../widgets/base/qkd_text.dart';

class LqPushConfigPage extends StatefulWidget {
  const LqPushConfigPage({super.key});

  @override
  State<LqPushConfigPage> createState() => _LqPushConfigPageState();
}

class _LqPushConfigPageState extends State<LqPushConfigPage> {
  LqConfig config = Get.find<ConfigService>().lqConfig;
  PushConfig pushConfig = Get.find<ConfigService>().pushConfig;
  ConfigService configService = Get.find<ConfigService>();
  bool showPushAll = false;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (configService.pushConfig.pushAll == 0) {
      showPushAll = true;
    }
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
      final permission = await Permission.notification.isGranted;
      if (!permission) {
        Get.dialog(const OpenNotificationDialog());
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Styles.appBar(title: QkdText('篮球比赛通知')),
      backgroundColor: Colours.scaffoldBg,
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 8),
        child: ListView(children: [
          const SizedBox(height: 10),
          if (showPushAll) ...[
            QkdWidget.switchCell('消息通知设置', pushConfig.pushAll == 1,
                onTap: () {
              pushConfig.pushAll = (pushConfig.pushAll == 1 ? 0 : 1);
              update();
              configService.update(ConfigType.pushAll, pushConfig.pushAll);
            }),
            separateTitle('开启后，您可以接收到最新动态消息'),
          ],
          if (pushConfig.pushAll == 1) ...[
            QkdWidget.switchCell('关注篮球比赛', config.lqPush1 == 1, onTap: () {
              config.lqPush1 = (config.lqPush1 == 1 ? 0 : 1);
              update();
              configService.update(ConfigType.lqPush1, config.lqPush1);
            }),
            separateTitle('开启后，您可以接收到已关注比赛的最新消息动态'),
            if (config.lqPush1 == 1) basketPushConfig()
          ]
        ]),
      ),
    );
  }

  Widget basketPushConfig() {
    return Styles.card(
        color: Colours.white,
        child: Column(children: [
          QkdWidget.switchCell('开赛（赛前5分钟）', config.lqPush2 == 1,
              isCard: false, onTap: () {
            config.lqPush2 = (config.lqPush2 == 1 ? 0 : 1);
            update();
            configService.update(ConfigType.lqPush2, config.lqPush2);
          }),
          QkdWidget.seperateLine(),
          QkdWidget.switchCell('赛果', config.lqPush3 == 1, isCard: false,
              onTap: () {
            config.lqPush3 = (config.lqPush3 == 1 ? 0 : 1);
            update();
            configService.update(ConfigType.lqPush3, config.lqPush3);
          })
        ]));
  }

  Widget separateTitle(String title) {
    return Padding(
      padding: const EdgeInsets.only(left: 16, top: 6, bottom: 12),
      child: QkdText(title, color: Colours.grey66, fontSize: 13),
    );
  }
}
